// TCPSocketClientDlg.cpp

#include "stdafx.h"
#include "TCPSocketClientDlg.h"
#include "afxdialogex.h"
#include "TCPSocketDef.h"
#include "TCPSocketClient.h"

TCPSocketClientDlg* TCPSocketClientDlg::m_pSocketClientDlg = nullptr;

IMPLEMENT_DYNAMIC(TCPSocketClientDlg, CDialogEx)
BEGIN_MESSAGE_MAP(TCPSocketClientDlg, CDialogEx)
    ON_BN_CLICKED(IDC_OPEN_CLIENT, &TCPSocketClientDlg::OnBnClickedOpenClient)
    ON_BN_CLICKED(IDC_START_TRANSFER, &TCPSocketClientDlg::OnBnClickedStartTransfer)
    ON_BN_CLICKED(IDC_CLOSE_CLIENT, &TCPSocketClientDlg::OnBnClickedCloseClient)
    ON_BN_CLICKED(IDC_CANCEL_CLIENT, &TCPSocketClientDlg::OnBnClickedCancelClient)
END_MESSAGE_MAP()

TCPSocketClientDlg::TCPSocketClientDlg(CWnd* pParent/* = NULL*/)
    : CDialogEx(TCPSocketClientDlg::IDD, pParent)
{
}

TCPSocketClientDlg::~TCPSocketClientDlg()
{
}

void TCPSocketClientDlg::UnInitialize()
{
    if (m_spSocketClient.get() != nullptr)
    {
        AfxMessageBox(L"Please close client TCP.");

        return;
    }
    if (m_pSocketClientDlg)
    {
        delete m_pSocketClientDlg;
        m_pSocketClientDlg = nullptr;
    }
}

void TCPSocketClientDlg::DoDataExchange(CDataExchange* pDX)
{
    CDialogEx::DoDataExchange(pDX);
}

TCPSocketClientDlg* TCPSocketClientDlg::GetInstance(CWnd* pParent/* = NULL*/)
{
    if (m_pSocketClientDlg == nullptr)
    {
        m_pSocketClientDlg = new TCPSocketClientDlg(pParent);
    }

    return m_pSocketClientDlg;
}

BOOL TCPSocketClientDlg::PreTranslateMessage(MSG* pMsg)
{
    if(pMsg->message==WM_KEYDOWN)
    {
        if((pMsg->wParam == VK_RETURN) || (pMsg->wParam == VK_ESCAPE))
        {
            UnInitialize();

            return true;
        }
    }

    return CDialogEx::PreTranslateMessage(pMsg);
}

BOOL TCPSocketClientDlg::IsInitialize()
{
    if (m_pSocketClientDlg == nullptr)
    {
        return FALSE;
    }

    return TRUE;
}

void TCPSocketClientDlg::SetCloseCallBack(CLOSE_CLIENT_DIALOG_CALLBACK callbackCloseDialog)
{
    m_callbackCloseDialog = callbackCloseDialog;
}

void TCPSocketClientDlg::OnBnClickedOpenClient()
{
    m_spSocketClient.release();
    m_spSocketClient.reset(new TCPSocketClient());
    DWORD dw = m_spSocketClient.get()->OpenTCP(this, "127.0.0.1", 10001);
    if (dw == 1)
    {
        AfxMessageBox(L"Open TCP success.");
    }
    else if (dw == -1)
    {
        AfxMessageBox(L"OpenTCP() Parameter error.");
    }
    else if (dw == -2)
    {
        AfxMessageBox(L"WSAStartup() failed.");
    }
    else if (dw == -3)
    {
        AfxMessageBox(L"socket() failed.");
    }
    else if (dw == -4)
    {
        AfxMessageBox(L"connect() failed.");
    }
    else
    {
        AfxMessageBox(L"Undefined error.");
    }

    GetDlgItem(IDC_OPEN_CLIENT)->EnableWindow(FALSE);
    GetDlgItem(IDC_START_TRANSFER)->EnableWindow(TRUE);
    GetDlgItem(IDC_CLOSE_CLIENT)->EnableWindow(TRUE);
    GetDlgItem(IDC_CANCEL_CLIENT)->EnableWindow(FALSE);
}

void TCPSocketClientDlg::OnBnClickedStartTransfer()
{
    GetDlgItem(IDC_OPEN_CLIENT)->EnableWindow(FALSE);
    GetDlgItem(IDC_START_TRANSFER)->EnableWindow(FALSE);
    GetDlgItem(IDC_CLOSE_CLIENT)->EnableWindow(FALSE);
    GetDlgItem(IDC_CANCEL_CLIENT)->EnableWindow(FALSE);

    DWORD dw = m_spSocketClient.get()->SendData("Hello");
    if (dw == -1)
    {
        AfxMessageBox(L"send() failed.");
    }
    else if (dw == -2)
    {
        AfxMessageBox(L"Not all send.");
    }
    else
    {
        CString str;
        str.Format(L"Send success, send 5 characters.");
        AfxMessageBox(str);
    }

    GetDlgItem(IDC_OPEN_CLIENT)->EnableWindow(FALSE);
    GetDlgItem(IDC_START_TRANSFER)->EnableWindow(TRUE);
    GetDlgItem(IDC_CLOSE_CLIENT)->EnableWindow(TRUE);
    GetDlgItem(IDC_CANCEL_CLIENT)->EnableWindow(FALSE);
}

void TCPSocketClientDlg::OnBnClickedCloseClient()
{
    DWORD dw = m_spSocketClient.get()->CloseTCP();
    if (dw == 1)
    {
        m_spSocketClient.release();
        AfxMessageBox(L"Close TCP success.");
    }
    else if (dw == -1)
    {
        AfxMessageBox(L"closesocket() failed.");
    }
    else if (dw == -2)
    {
        AfxMessageBox(L"WSACleanup() failed.");
    }
    else
    {
        AfxMessageBox(L"Undefined error.");
    }

    GetDlgItem(IDC_OPEN_CLIENT)->EnableWindow(TRUE);
    GetDlgItem(IDC_START_TRANSFER)->EnableWindow(FALSE);
    GetDlgItem(IDC_CLOSE_CLIENT)->EnableWindow(FALSE);
    GetDlgItem(IDC_CANCEL_CLIENT)->EnableWindow(TRUE);
}

void TCPSocketClientDlg::OnBnClickedCancelClient()
{
    m_callbackCloseDialog();

    UnInitialize();
}
